<template>
  <el-dialog
    v-model="dialogShow"
    :close-on-click-modal="false"
    :destroy-on-close="false"
    custom-class="mydialog"
    width="30%"
    title="下架产品"
    @close="close"
  >
    <el-form
      ref="formRef"
      :model="soldOutForm"
      :label-width="formLabelWidth"
      size="small"
      :rules="formRules"
    >
      <el-form-item
        label="下架原因"
        prop="productDownReason"
      >
        <el-input
          v-model="soldOutForm.productDownReason"
          type="textarea"
          placeholder="请输入下架原因"
          autocomplete="off"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div
        class="dialog-footer"
        style="display:flex;justify-content:center;align-items:center;"
      >
        <el-button
          class="border-btn"
          size="small"
          @click="close"
        >取消</el-button>
        <el-button
          class="main-btn"
          size="small"
          type="primary"
          @click="confirm"
        >确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {defineComponent, getCurrentInstance, inject, reactive, ref} from 'vue'

export default defineComponent({
  name: 'SoldOutProduct',
  emits: ['soldOutSuccess'],
  setup(props, context) {
    const api = inject('api')
    const formRef = ref(null)
    const currentInstance = getCurrentInstance()
    var soldOutForm = reactive({
      id: 0,
      productDownReason: '',
    })
    var formRules = reactive({
      productDownReason: [
        { required: true, message: '请输入下架原因', trigger: 'blur' },
      ],
    })
    var formLabelWidth = ref(100)
    var dialogShow = ref(false)
    const open = (productId) => {
      soldOutForm.id = productId
      dialogShow.value = true
    }
    const close = () => {
      dialogShow.value = false
      formRef.value.resetFields()
    }
    const confirm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          api.product.DOWN_PRODUCT(soldOutForm).then((res) => {
            if (res.code === 0) {
              currentInstance.proxy.customMessage({
                type: 'success',
                message:
                  '提交成功，已创建[产品下架]审批，可在[流程审批-我发起的]查看审批进程',
              })
              context.emit('soldOutSuccess')
              close()
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
    return {
      formRef,
      soldOutForm,
      formRules,
      formLabelWidth,
      dialogShow,
      open,
      close,
      confirm,
    }
  },
})
</script>
